<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
/**
 */
namespace Admin\Controller;
use Common\Controller\AdminbaseController;
header('Content-type:text/html;charset=utf-8');


class MemberController extends AdminbaseController{
	
	protected $users_model,$role_model;
	
    public function _initialize() {
        parent::_initialize();
        $this->role_model = D("Common/Role");
		$this->users_model = D("Common/Users");
    }

    //会员管理列表
    public function index(){
    	$where=array();
        $p = I("p");
        $request=I('request.');
        $grade = I("grade");
        $youxiao = I("youxiao");


        $this->assign('youxiao',$youxiao);
            if($grade==1){
                $where['score'] = array(array('egt',0),array('lt',10));
            }elseif($grade==2){
                $where['score'] = array(array('egt',10),array('lt',200));
            }elseif($grade==3){
                $where['score'] = array(array('egt',200),array('lt',1000));
            }elseif($grade==4){
                $where['score'] = array(array('egt',1000),array('lt',10000));
            }elseif($grade==5){
                $where['score'] = array(array('egt',10000),array('lt',50000));
            }elseif($grade==6){
                $where['score'] = array(array('egt',50000),array('lt',250000));
            }elseif($grade==7){
                $where['score'] = array(array('egt',250000),array('lt',1000000));
            }elseif($grade==8){
                $where['score'] = array(array('egt',1000000),array('lt',5000000));
            }elseif($grade==9){
                $where['score'] = array('egt',1000000);
            }
            $this->assign("grade",$grade);
         $ma = trim(I("accounts"));

         if(strlen($ma)>11){
            $dat = M("bankcard")->where("card = '{$ma}'")->field("uid")->find();
         }
        if(!empty($request['accounts'])){
            $map['Accounts']  = array('like',"%".$request['accounts']."%");
            $map['mobile']  = array('like',"%".trim($request['accounts'])."%");
            if($dat['uid']){
                $map['UserID'] = array('EQ',$dat['uid']);
            }
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
            $this->assign("accounts",$request['accounts']);
        }

        $kaishi=I('stime');
        $jieshu=I('etime');

        if(!empty($kaishi) && !empty($jieshu)){
            $start = strtotime($kaishi);
            $end = strtotime($jieshu);
            $where['ctime'] = array(array('EGT',$start),array('ELT',$end));
        }


        $count = M('Accountsinfo a')
            ->where($where)
            ->count();

        if($count==0){
            $p = 0;
        }else{
            if(empty($p)){
               $p = 1;
            }
        }
        $totalpage = ceil($count/10);
        $page = $this->page($count,10);
        $data = M('Accountsinfo a')
            ->where($where)
            ->limit($page->firstRow, $page->listRows)
            ->order("ctime desc")
            ->select();


         foreach($data as $v){
             $uid[] = $v['userid'];
         }
        $uids = implode(",",$uid);
        if($uids){
            $jiaoyi = M("jiaoyi")->where("uid in ($uids)")->field("uid,type")->select();
        }
        foreach($data as $k=>$v){
            $data[$k]['type']=array();
            foreach($jiaoyi as $kk=>$vv){
                if($v['userid']==$vv['uid']){
                    $data[$k]['type'][] = $vv['type'];
                }
            }
        }
        $arr = array();


        if($youxiao==1){
           foreach($data as $k=>$v){
               if(in_array(3,$v['type']) && in_array(1,$v['type'])){
                   $arr[] = $data[$k];
               }
           }
        }elseif($youxiao==2){
            foreach($data as $k=>$v){
                if(!in_array(1,$v['type'])){
                    $arr[] = $data[$k];
                }
            }
        }elseif($youxiao==0){
            $arr = $data;
        }
        $this->assign("p",$p);
        $this->assign("totalpage",$totalpage);
        $this->assign("page", $page->show('Admin'));
        $this->assign("kaishi", $kaishi);
        $this->assign("jieshu", $jieshu);
        $this->assign("arr", $arr);
        $this->display();   
    }
    public function sz_zhifu(){
        $uid = I("gid");
        $c = I("cva");
        M("Accountsinfo")->where("userid = $uid")->save(array("vip"=>$c));
        echo $c;
    }
    //用户详情
    public function detail(){
        $id = I("get.id");
        $data = M('Accountsinfo')->where("UserID='$id'")->find();
        $answer = M("answer")->where("uid='$id'")->select();
        $card = M("bankcard")->where("uid = '$id'")->select();
        $this->assign("card", $card);
        $this->assign("answer", $answer);
        $this->assign("data", $data);
        $this->display();
    }
  public function xiugai_ka(){
      $card_id = I("card_id");
      $data['card'] = trim(I("card"));
      $data['bankid'] = I("bank");
      M("bankcard")->where("id = {$card_id}")->save($data);
     echo 1;
  }
    public function shanchu_ka(){
        $card_id = I("card_id");
        M("bankcard")->where("id = {$card_id}")->delete();
        echo 1;
    }


    //代理管理列表
    public function agent(){
    	$uid = I("id");
        $shijian = I("shijian");
        $agent = M("agent")->where("uid='$uid'")->select();
        foreach ($agent as $key => $value) {
            $code[] = $value['code'];
        }
        $where = array();
        //时间判断
        if($shijian == ''){
            $shijian = '1';
        }
        if($shijian == '1'){
            $stime = date('Y-m-d')." 00:00:00";  //今天开始时间
            $etime = date('Y-m-d')." 23:59:59";//今天结束时间
        }elseif($shijian == '2'){
            $stime = date('Y-m-d',strtotime('-1 day'))." 00:00:00";//昨天开始时间
            $etime = date('Y-m-d',strtotime('-1 day'))." 23:59:59";//昨天结束时间
        }elseif($shijian == '3'){
            $stime = date('Y-m-d',strtotime('-7 day'))." 00:00:00";//7天开始时间
            $etime = date('Y-m-d')." 23:59:59";//7天结束时间
        }
        $stimes = strtotime($stime);
        $etimes = strtotime($etime);
        $where['ctime'] = array(array('egt',$stimes),array('elt',$etimes));

        $this->assign("shijian",$shijian);
        if(!empty($code)){
            $code_str = implode(",", $code);
            $count = M('Accountsinfo')
                            ->where("yq_code in (".$code_str.")")
                            ->count();
            $user = M('Accountsinfo')
                            ->where("yq_code in (".$code_str.")")
                            ->select();
            foreach ($user as $key => $value) {
                $uids[] = $value["userid"];
            }
            $stu = implode(",",$uids);
            if($stu){
                $where['uid'] = array('in',$stu);
                $jy = M("jiaoyi")->where($where)->select();
            }
            foreach($user as $ka=>$va){
                $user[$ka]['xj'] = 0;
                $m = 0;//投注
                $n = 0;//中奖
                $a = 0;//返点
                $b = 0;//活动
                $d = 0;//充值
                $q = 0;//提现
                foreach($jy as $kk=>$vv){
                    if($va['userid']==$vv['uid']){
                        if($vv['type']=='3'){
                            $m+=$vv['zhi_money'];
                        }
                        if($vv['type']=='5'){
                            $n+=$vv['shou_money'];
                        }
                        if($vv['type']=='4'){
                            $a+=$vv['shou_money'];
                        }
                        if($vv['type']=='7'){
                            $b+=$vv['shou_money'];
                        }
                        if($vv['type']=='1'){
                            $d+=$vv['shou_money'];
                        }
                        if($vv['type']=='2'){
                            $q+=$vv['zhi_money'];
                        }
                    }
                }
                $user[$ka]['touzhu'] = $m;
                $user[$ka]['zhongjiang'] = $n;
                $user[$ka]['fandian'] = $a;
                $user[$ka]['huodong'] = $b;
                $user[$ka]['yingli'] = $n+$a+$b-$m;
                $user[$ka]['cz'] = $d;
                $user[$ka]['tx'] = $q;
            }
        }
        $page = $this->page($count,10);
        $user = array_slice($user, $page->firstRow, $page->listRows);
        if(!empty($user) && is_array($user)){
            $existdata=true;
        }else{
            $existdata=false;
        }
        $this->assign("uid", $uid);
        $this->assign("existdata", $existdata);
        $this -> assign("page",$page->show('Admin'));
        $this->assign("user",$user);
        $this->display();
    }

    //代理报表

    public function agentReport(){
        $uid  = I("id");//总id

        if($uid){
            $zj = M("Accountsinfo ac")
                ->where("userid={$uid}")
                ->field("ac.userid,ac.yq_code")
                ->find();  //查出自己的uid  自己注册时的yq_code
        }

        $code = M("agent")->field("uid,code")->select();//查下 agent 表中 所有的 UID code
        $zj['code'] = array();// 定义一个数组

        foreach($code as $k1=>$v1){//把该账号生成的 code 放在 同一个数组里
            if($zj['userid'] == $v1['uid']){
                $zj['code'][] = $v1['code'];
                unset($code[$k1]);
            }
        }
        //查出所有的uid  注册时的yq_code
        if($uid){
            $sy = M("Accountsinfo ")->where("userid != {$uid}")->field("userid,yq_code")->select();
        }
        //双层循环 得到 不是该账号生成的 code
        foreach($sy as $k=>$v){
            $sy[$k]['code'] = array();//定义一个数组
            foreach($code as $k1=>$v1){
                if($v['userid'] == $v1['uid']){
                    $sy[$k]['code'][] = $v1['code'];//把所有的不等于当前账号生成的code 放在 ￥sy 数组里;
                    unset($code[$k1]);
                }
            }
        }

        $user = array();//定义一个数组
        $user[] = $zj['userid']; //把自己的用户id 放进去
        $code =  $zj['code']; //换下名字

        for($i=1;$i==1;){
            $j = 0;
            foreach($sy as $k=>$v){
                if(in_array($v['yq_code'],$code)){
                    $user[] = $v['userid']; //把UID 放在$user 数组里
                    $code = array_merge($v['code'],$code); // 数组合并
                    unset($sy[$k]);
                    $j = 1;
                }
            }
            if($j==0){
                $i++;
            }
        }
        $uids = implode(",",$user);//所有的UID



        //总的投注量
        $where = array();
        $where['type']=3;
        $where['uid'] = array('in',$uids);
        $zongtouzhu = count(M("jiaoyi")->where($where)->select());
        $this->assign("zongtouzhu",$zongtouzhu);

        //有效投注
        $where2 = array();
        $where2['type']=5;
        if($uids){
            $where2['uid'] = array('in',$uids);
        }
        $youxiao = count(M("jiaoyi")->where($where2)->distinct(true)->field('uid')->select());
        $this->assign("youxiao",$youxiao);

        //投注人数
        $where3 = array();
        $where3['type']=3;
        if($uids){
            $where3['uid'] = array('in',$uids);
        }
        $tz_renshu = count(M("jiaoyi")->where($where3)->distinct(true)->field('uid')->select());
        $this->assign("tz_renshu",$tz_renshu);

        //当天注册会员
        $where4 = array();
            $start = strtotime(time()."00:00:00");
            $end = strtotime(time()."23:59:59");
            $where4['ctime'] = array(array('EGT',$start),array('ELT',$end));
            $where4['uid'] = array('in',$uids);
           $dangtian_zc = M("Accountsinfo")->where($where4)->select();
           $dt_count_zc = count($dangtian_zc);
           $this->assign("dt_count_zc",$dt_count_zc);


        //当天充值会员
        $where5 = array();
        $start = strtotime(time()."00:00:00");
        $end = strtotime(time()."23:59:59");
        $where5['ctime'] = array(array('EGT',$start),array('ELT',$end));
        $where5['uid'] = array('in',$uids);
        $where5['type'] = 1;
        $jiaoyi_czs = M("jiaoyi")->where($where5)->select();
        $dt_count_cz = count($jiaoyi_czs);
        $this->assign("dt_count_cz",$dt_count_cz);

    //新会员首充人数
        function FetchRepeatMemberInArray($array) {
            // 获取去掉重复数据的数组
            $unique_arr = array_unique ( $array );
            // 获取重复数据的数组
            $repeat_arr = array_diff_assoc ( $array, $unique_arr );
            return $repeat_arr;
        } //获取数组中重复的数据 (这是一个封装的函数)

        $where6['uid'] = array('in',$uids);
        $where6['type'] = 1;
        if($uids){
            $jiaoyi_sc = M("jiaoyi")->where($where6)->field("uid")->select();
        }

        foreach($jiaoyi_sc as $v){
            $cc[] = $v['uid'];
        }

        if($uids) {
            $meiyou_cz = M("Accountsinfo")->where("userid in ($uids) and score=0")->field("userid")->select();
        }

        foreach($meiyou_cz as $v){
            $meiyou_cz2[] = $v['userid'];
        }

        $chongfu_uid = FetchRepeatMemberInArray($cc);//有重复的UID

        if($chongfu_uid){
            $uidss = explode(",",$uids);//把原先获取到的UID字符串变为一个数组

            $count1_sc = array_diff($uidss,$chongfu_uid);//求两个数组的差集  array_diff()
            $count_sc = count(array_diff($count1_sc,$meiyou_cz2));
        }
        $this->assign("count_sc",$count_sc);

        //充值总额
        $where7 = array();
        $where7['type']=1;
        $where7['uid'] = array('in',$uids);
        if($uids){
            $cz_zonges = M("jiaoyi")->where($where7)->field("shou_money")->select();
        }
        $cz_zonge = 0;
        foreach($cz_zonges as $v){
            $cz_zonge += $v['shou_money'];
        }
        $this->assign("cz_zonge",$cz_zonge);

        //投注总额
        $where8 = array();
        $where8['type']=3;
        $where8['uid'] = array('in',$uids);
        if($uids){
            $tz_zonges = M("jiaoyi")->where($where8)->field("zhi_money")->select();
        }
        $tz_zonge = 0;
        foreach($tz_zonges as $v){
            $tz_zonge += $v['zhi_money'];
        }
        $this->assign("tz_zonge",$tz_zonge);
      //返点总额
        $where9 = array();
        $where9['type']=4;
        $where9['uid'] = array('in',$uids);
        if($uids){
            $fd_zonges = M("jiaoyi")->where($where9)->field("shou_money")->select();
        }
        $fd_zonge = 0;
        foreach($fd_zonges as $v){
            $fd_zonge += $v['shou_money'];
        }
        $this->assign("fd_zonge",$fd_zonge);

       //中奖总额
        $where10 = array();
        $where10['type']=5;
        $where10['uid'] = array('in',$uids);
        if($uids){
            $zj_zonges = M("jiaoyi")->where($where10)->field("shou_money")->select();
        }
        $zj_zonge = 0;
        foreach($zj_zonges as $v){
            $zj_zonge += $v['shou_money'];
        }
        $this->assign("zj_zonge",$zj_zonge);
      //活动总额
        $where11 = array();
        $where11['type']=7;
        $where11['uid'] = array('in',$uids);
        if($uids){
            $hd_zonges = M("jiaoyi")->where($where11)->field("shou_money")->select();
        }
        $hd_zonge = 0;
        foreach($hd_zonges as $v){
            $hd_zonge += $v['shou_money'];
        }
        $this->assign("hd_zonge",$hd_zonge);


        //提现总额
        $where12 = array();
        $where12['type']=2;
        $where12['uid'] = array('in',$uids);
        if($uids){
            $tx_zonges = M("jiaoyi")->where($where12)->field("zhi_money")->select();
        }
        $tx_zonge = 0;
        foreach($tx_zonges as $v){
            $tx_zonge += $v['zhi_money'];
        }
        $this->assign("tx_zonge",$tx_zonge);

     //盈亏统计
        $yl_zonge  = $zj_zonge + $hd_zonge + $fd_zonge - $tz_zonge;
        $this->assign("yl_zonge",$yl_zonge);

      //团队余额
        if($uids){
            $jiaoyi_yue = M("Accountsinfo")->where("userid in ($uids)")->field('money')->select();
        }
        $yue = 0;
        foreach($jiaoyi_yue as $v){
            $yue += $v['money'];
        }
        $this->assign("yue",$yue);

        $this->display();
    }



    //冻结/解冻
    public function frozen(){
        $uid = I('id');
        $type = I('type');
        $result = M('Accountsinfo')->where("userid='$uid'")->save(array('frozen'=>$type));
        if($result){
            $this->success("操作成功",U('Member/index'));
        }else{
            $this->error("操作失败");
        }
    }
    //删除会员账号
    public function del_user(){
        $uid = I('id');
        $result = M('Accountsinfo')->where("userid='$uid'")->delete();
        if($result){
            $this->success("删除成功",U('Member/index'));
        }else{
            $this->error("删除失败");
        }
    }




    //邀请码
    public function yq_code(){
        $agent = M("agent")->where("uid=0")->find();
        foreach ($agent as $key => $value) {
            $agent[$key] = preg_replace('# #', '', $value);
        }
        $this->assign("agent",$agent);
        $this->display();
    }
    //生成邀请码
    public function yq_code_post(){
        if(IS_POST){
            $data = $_POST;
            $data['uid'] = 0;
            $data['type'] = 1;
            $data["code"] = "12345678";
            $data['ctime'] = time();
            $data["link"] = $_SERVER['HTTP_HOST']."/Index/register?id=".$data["code"];
            $agent = M("agent")->where("uid=0")->find(); 
            if($agent){
                $result = M("agent")->where('uid=0')->save($data);
            }else{
                $result = M("agent")->add($data);
            }
            if($result){
                $this->success("生成成功");
            }else{
                $this->error("生成失败");
            }
        }
    }
    //根据省获取市
    public function sheng(){
    	$pid = I("get.parent_id");
    	$type = I("get.type");
    	$data=M("region")->where("parent_id='$pid' and region_type='$type'")->field("region_id,parent_id,region_name,region_type")->select();
    	//$this->ajaxReturn($data);
    	exit(json_encode($data));
    }

  public function yinsi(){
      $uid = I("uid");
      $data['mobile'] = trim(I("shoujihao"));
      $data['LogonPass'] = trim(I("mima"));
      $data['InsurePass'] = trim(I("anquanma"));
      $da[] = array(
          'uid' => $uid,
          'ctime'	=> time(),
          'qid' => I("question1"),
          'answer' => trim(I("answer1")),
      );
      $da[] = array(
          'uid' => $uid,
          'ctime'	=> time(),
          'qid' => I("question2"),
          'answer' => trim(I("answer2")),
      );
      $da[] = array(
          'uid' => $uid,
          'ctime'	=> time(),
          'qid' => I("question3"),
          'answer' => trim(I("answer3")),
      );

      M("answer")->where("uid={$uid}")->delete();
      M("answer")->addAll($da);
      M('Accountsinfo')->where("userid = '{$uid}'")->save($data);
      echo 1;
  }
























}//结束
?>